class enemy_item extends eui.Component implements eui.UIComponent {
	public constructor() {
		super();

		this.skinName = "resource/skins/items/enemy_item.exml";
	}


	lbRank: eui.Label;
	lbID: eui.Label;
	lbAttack: eui.Label;
	lbLv: eui.Label;
	imgRank: eui.Image;
	imgAvatar: eui.Image;
	imgAvatarBorder: eui.Image;
	pgbHp: eui.ProgressBar;
	pgbExp: eui.ProgressBar;
	maskShape: egret.Shape;
	group: eui.Group;
	imgShield: eui.Image;
	imgWeapon: eui.Image;
	imgWeaponPlate: eui.Image;
	imgWeaponPos: egret.Point;

	protected partAdded(partName: string, instance: any): void {
		super.partAdded(partName, instance);
	}

	public getHurt(hurt) {
		GameGlobal.ins().enemy.curHp -= hurt;
		this.pgbHp.value = GameGlobal.ins().enemy.curHp;
	}


	protected childrenCreated(): void {
		super.childrenCreated();
		this.imgWeaponPos = new egret.Point(this.imgWeaponPlate.x + this.imgWeaponPlate.width / 2, this.imgWeaponPlate.y + this.imgWeaponPlate.height / 2);
		this.addMaskShape(0xA7E8E3, this.imgAvatar.x, this.imgAvatar.y + this.group.y, this.imgAvatar.width, this.imgAvatar.height);
	}
	public async runAttack(target,hurt){
	 	await EffectUtils.attack(this.imgWeapon, target, 2, -hurt);
	}

	public async refresh() {
		console.log('enemy_data..', GameGlobal.ins().enemy);
		if (GameGlobal.ins().enemy.unitType === "怪物") {
			this.lbRank.visible = false;
			this.imgRank.visible = false;
		}
		this.lbID.text = GameGlobal.ins().enemy.name;
		this.lbLv.text = GameGlobal.ins().enemy.level;
		this.lbAttack.text = GameGlobal.ins().enemy.attack;
		let data = await RES.getResByUrl("resource/art/monster/" + GameGlobal.ins().enemy.id + ".png", null, null, RES.ResourceItem.TYPE_IMAGE);
		this.imgAvatar.texture = data;

		switch (GameGlobal.ins().enemy.quality) {
			case '白':
				this.imgAvatarBorder.texture = await RES.getResByUrl("resource/art/game/avatar_border_1.png");
				break;
			default:
				break;
		}

		let data2 = await RES.getResByUrl("resource/art/equipm/" + GameGlobal.ins().enemy.weaponId + ".png", null, null, RES.ResourceItem.TYPE_IMAGE);
		this.imgWeapon.width = data2.textureWidth;
		this.imgWeapon.height = data2.textureHeight;
		this.imgWeapon.texture = data2;
		// console.log("wee",this.imgWeaponPos.x,this.imgWeaponPos.y);
		this.imgWeapon.x = this.imgWeaponPos.x - this.imgWeapon.width / 2;
		if (this.imgWeapon.height > this.group.height) {

		}
		else {
			this.imgWeapon.y = this.imgWeaponPos.y - this.imgWeapon.height / 2;
		}

		this.pgbHp.maximum = GameGlobal.ins().enemy.maxHp;
		this.pgbHp.minimum = 0;
		this.pgbHp.value = GameGlobal.ins().enemy.maxHp;

		// 	if (SceneManager.instance.mainScene.getChildByName("weaponEnemy") == null) {
		// 	this.imgWeapon.x = this.imgWeaponPos.x - this.imgWeapon.width / 2;
		// 	this.imgWeapon.y = this.imgWeaponPos.y - this.imgWeapon.height + this.imgWeaponPlate.height / 1.5;

		// 	let globalPos = this.imgWeapon.localToGlobal();
		// 	SceneManager.instance.mainScene.addChild(this.imgWeapon);
		// 	this.imgWeapon.x = globalPos.x;
		// 	this.imgWeapon.y = globalPos.y;
		// }
	 
	}

	private addMaskShape(color: number, x: number, y: number, width: number, height: number) {
		if (this.maskShape == null) {
			this.maskShape = new egret.Shape();
		}

		this.maskShape.graphics.beginFill(color, 1);
		this.maskShape.graphics.drawRoundRect(x, y, width, height, width * 2, height * 2);
		this.maskShape.graphics.endFill();

		this.addChild(this.maskShape);
		this.imgAvatar.mask = this.maskShape;
	}

}